package com.itxiaox.xview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.view.View;

import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

/**
 * Desc:九宫格分割线   无最右边与最下边线
 * Author:zhangcx
 * Data: 2018/8/1  11:56
 */
public class GridDividerItemDecoration extends RecyclerView.ItemDecoration {
    private static final int[] ATTRS = new int[]{android.R.attr.listDivider};
    private Drawable mDivider;
    private Context mContext;

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public GridDividerItemDecoration(Context context) {
        mContext = context;
//        final TypedArray a = context.obtainStyledAttributes(ATTRS);
        mDivider = context.getDrawable(R.color.line_pw_divider);
//        a.recycle();
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        drawHorizontal(c, parent);
        drawVertical(c, parent);
    }

    private int getSpanCount(RecyclerView parent) {
        // 列数
        int spanCount = -1;
        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {
            spanCount = ((GridLayoutManager) layoutManager).getSpanCount();
        }
        return spanCount;
    }

    public void drawHorizontal(Canvas c, RecyclerView parent) {
        int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View child = parent.getChildAt(i);
            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
                    .getLayoutParams();
            final int left = child.getLeft() - params.leftMargin;
            final int right = child.getRight() + params.rightMargin
                    + mDivider.getIntrinsicWidth();
            final int top = child.getBottom() + params.bottomMargin;
            final int bottom = top + mDivider.getIntrinsicHeight();
//            if(mContext.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT){
//                params.width = UIParamUtils.getReviewWidth() / getSpanCount(parent);
//                child.setLayoutParams(params);
//            }

            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(c);
        }
    }

    public void drawVertical(Canvas c, RecyclerView parent) {
        final int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View child = parent.getChildAt(i);

            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
                    .getLayoutParams();
            final int top = child.getTop() - params.topMargin;
            final int bottom = child.getBottom() + params.bottomMargin;
            final int left = child.getRight() + params.rightMargin;
            final int right = left + mDivider.getIntrinsicWidth();

            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(c);
        }
    }

//    /**
//     * 是否是最后一行
//     */
//    private boolean isLastRow(int itemPosition, RecyclerView parent) {
//        int spanCount = getSpanCount(parent);
//        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
//        //有多少列
//        if (layoutManager instanceof GridLayoutManager) {
//            int childCount = parent.getAdapter().getItemCount();
//
//            double count = Math.ceil((double) childCount / (double) spanCount);//总行数
//            double currentCount = Math.ceil((double) (itemPosition + 1) / spanCount);//当前行数
//
//            //最后当前数量小于总的
//            if (currentCount < count) {
//                return false;
//            }
//        }
//        return true;
//    }
//
//
//    /**
//     * 判断是否是最后一列
//     */
//    private boolean isLastColum(int itemPosition, RecyclerView parent) {
//        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
//        //有多少列
//        if (layoutManager instanceof GridLayoutManager) {
//            int spanCount = getSpanCount(parent);
//            if ((itemPosition + 1) % spanCount == 0) {//因为是从0可以所以要将ItemPosition先加1
//                return true;
//            }
//        }
//        return false;
//    }
//
//    @Override
//    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State
//            state) {
//        super.getItemOffsets(outRect, view, parent, state);
//        if (isLastRow(parent.getChildLayoutPosition(view), parent))// 如果是最后一行，则不需要绘制底部
//        {
//            outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
//        }
//
//        if (isLastColum(parent.getChildLayoutPosition(view), parent))// 如果是最后一列，则不需要绘制右边
//        {
//            outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
//        }
//    }
}